package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.rank.ServerRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.cross.CrossActivityServerRankListModel;
import com.yanqu.road.logic.cross.CrossRankListModel;
import com.yanqu.road.logic.cross.CrossUnionActivityRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityRankMgr extends TempMgr {
    /**
     * 各个游戏服的个人排名 区服，活动，数据
     */
    private static Map<Long, Map<Integer, CrossRankListModel>> allCrossRankListModelMap;
    /**
     * 活动个人排名 活动，数据
     */
    private static Map<Integer, CrossActivityRankListModel> crossActivityRankListModelMap;
    /**
     * 区服排名 活动，数据
     */
    private static Map<Integer, CrossActivityServerRankListModel> crossActivityServerRankListModelMap;

    /**
     * 商会排名（冲榜、争霸） 活动，数据
     */
    private static Map<Integer, CrossUnionActivityRankListModel> crossActivityUnionRankListModelMap;
    /**
     * 活动个人排名 活动，条件类型，榜单
     */
    private static Map<Integer, Map<Integer, CrossActivityRankListModel>> crossActivityUserRankListModelMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for(Map<Integer, CrossRankListModel> dataMap : allCrossRankListModelMap.values()) {
            for (CrossRankListModel rankListModel : dataMap.values()) {
                rankListModel.saveActivityRankList();
            }
        }
        for(CrossUnionActivityRankListModel rankListModel : crossActivityUnionRankListModelMap.values()){
            rankListModel.saveActivityRankList();
        }
        for(CrossActivityRankListModel rankListModel : crossActivityRankListModelMap.values()){
            rankListModel.saveCrossActivityRankList();
        }
        for(Map<Integer, CrossActivityRankListModel> rankMap : crossActivityUserRankListModelMap.values()){
            for(CrossActivityRankListModel rankListModel : rankMap.values()){
                rankListModel.saveNewCrossActivityRankList();
            }
        }

        long now = System.currentTimeMillis() / 1000;
        for (Map.Entry<Integer, CrossActivityRankListModel> entry : crossActivityRankListModelMap.entrySet()) {
            int activityId = entry.getKey();
            ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
            if (activityInfo != null && now > activityInfo.getEndShowTime() + 12 * DateHelper.HOUR_SECONDS) {
                getLogger().error("移除过期冲榜活动 {}", activityInfo.getActivityId());

                for (Map<Integer, CrossRankListModel> value : allCrossRankListModelMap.values()) {
                    value.remove(activityId);
                }
                crossActivityRankListModelMap.remove(activityId);
                crossActivityServerRankListModelMap.remove(activityId);
                crossActivityUnionRankListModelMap.remove(activityId);
                crossActivityUserRankListModelMap.remove(activityId);
            }
        }


        return true;
    }

    @Override
    public boolean reloadData() {
        allCrossRankListModelMap = new ConcurrentHashMap<>();
        initCrossRankListModelMap();

        //商会争霸
        crossActivityUnionRankListModelMap = new ConcurrentHashMap<>();
        initCrossUnionActivityRankListModelMap();

        crossActivityRankListModelMap = new ConcurrentHashMap<>();
        crossActivityServerRankListModelMap = new ConcurrentHashMap<>();
        initCrossActivityRankListModelMap();
        initCrossActivityUserRankListModelMap();

        crossActivityUserRankListModelMap = new ConcurrentHashMap<>();
        initNewCrossActivityRankListModelMap();
        return true;
    }

    private void initCrossUnionActivityRankListModelMap() {
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossUnionRankActivityMap();
        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, LinkedList<CrossUnionActivityRank>> crossUnionActivityRankListMap = RankBussiness.getCrossUnionActivityRankListMap(activityIdList);
        synchronized (crossActivityUnionRankListModelMap) {
            for (Map.Entry<Integer, LinkedList<CrossUnionActivityRank>> activityEntry : crossUnionActivityRankListMap.entrySet()) {
                CrossUnionActivityRankListModel rankListModel = new CrossUnionActivityRankListModel(activityEntry.getKey());
                ActivityInfo activityInfo = activityInfoMap.get(activityEntry.getKey());
                rankListModel.setActivityId(activityEntry.getKey());
                rankListModel.setRankNum(getMaxRank(activityInfo));
                rankListModel.setRankList(activityEntry.getValue());
                crossActivityUnionRankListModelMap.put(activityEntry.getKey(), rankListModel);
            }
        }
    }

    /**
     * 按活动设置及活动奖励，计算最大榜单排名人数
     * @param activityInfo
     * @return
     */
    public static int getMaxRank(ActivityInfo activityInfo) {
        int maxRank = 0;
        if(!StringUtils.isNullOrEmpty(activityInfo.getParam6())){
            maxRank = Integer.parseInt(activityInfo.getParam6());
        }
        int conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
        int maxRank2 = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        if (maxRank2 > maxRank) {
            maxRank = maxRank2;
        }
        if (maxRank <= 0) {
            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        }
        return maxRank;
    }

    private void initCrossRankListModelMap(){
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossRankActivityMap();
        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Long, Map<Integer, LinkedList<CrossUserRank>>> crossUserRankListMap = RankBussiness.getCrossActivityRankListMap(activityIdList);
        for(Map.Entry<Long, Map<Integer, LinkedList<CrossUserRank>>> dataEntry : crossUserRankListMap.entrySet()){
            for(Map.Entry<Integer, LinkedList<CrossUserRank>> tempEntry : dataEntry.getValue().entrySet()) {
                CrossRankListModel rankListModel = new CrossRankListModel(dataEntry.getKey(), tempEntry.getKey(), eBigRankType.Normal.getValue());
                ActivityInfo activityInfo = activityInfoMap.get(tempEntry.getKey());
                int maxRank = getMaxRank(activityInfo);
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(tempEntry.getValue());
                Map<Integer, CrossRankListModel> modelMap = getCrossRankListModelMap(dataEntry.getKey());
                if(null == modelMap){
                    modelMap = new ConcurrentHashMap<>();
                    allCrossRankListModelMap.put(dataEntry.getKey(), modelMap);
                }
                modelMap.put(tempEntry.getKey(), rankListModel);
            }
        }
    }

    private void initCrossActivityUserRankListModelMap(){
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossCommonActivityMap();
        activityInfoMap.putAll(ActivityMgr.getOpenHuaShiGangRankActivityMap());
        activityInfoMap.putAll(ActivityMgr.getOpenDunHuangShiKuRankActivityMap());

        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, LinkedList<CrossUserRank>> crossUserRankListMap = RankBussiness.getCrossActivityUserRankListMap(activityIdList);
        for (Map.Entry<Integer, LinkedList<CrossUserRank>> dataEntry : crossUserRankListMap.entrySet()) {
            CrossActivityRankListModel rankListModel = new CrossActivityRankListModel(dataEntry.getKey(), true);
            ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
            int conditionType = ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_CROSS;
            if(activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue() ||
                    activityInfo.getType() == eActivityType.CrossDunHuangShiKuActivity.getValue()){
                conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
            }
            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
            if (maxRank <= 0) {
                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
            }
            rankListModel.setRankNum(maxRank);
            rankListModel.setRankList(dataEntry.getValue());
            crossActivityRankListModelMap.put(dataEntry.getKey(), rankListModel);
        }
    }

    private void initNewCrossActivityRankListModelMap(){
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenNewRankActivityMap();
        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, Map<Integer, LinkedList<CrossUserRank>>> crossNewRankListMap = RankBussiness.getCrossActivityNewRankListMap(activityIdList);
        for(Map.Entry<Integer, Map<Integer, LinkedList<CrossUserRank>>> dataEntry : crossNewRankListMap.entrySet()) {
            if(!crossActivityUserRankListModelMap.containsKey(dataEntry.getKey())){
                synchronized (crossActivityUserRankListModelMap){
                    if(!crossActivityUserRankListModelMap.containsKey(dataEntry.getKey())){
                        crossActivityUserRankListModelMap.put(dataEntry.getKey(), new ConcurrentHashMap<>());
                    }
                }
            }
            for (Map.Entry<Integer, LinkedList<CrossUserRank>> tempEntry : dataEntry.getValue().entrySet()) {
                ActivityInfo activityInfo = activityInfoMap.get(dataEntry.getKey());
                CrossActivityRankListModel rankListModel;
                Comparator comparator = ActivityHelper.getUserRankSorter(activityInfo.getType(), tempEntry.getKey(), true);
                if(comparator == null){
                    rankListModel = new CrossActivityRankListModel(dataEntry.getKey(), true);
                }else {
                    rankListModel = new CrossActivityRankListModel(dataEntry.getKey(), true, comparator);
                }
                rankListModel.setConditionType(tempEntry.getKey());
                int maxRank = ActivityHelper.getMaxRankByConditionType(tempEntry.getKey(), ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                if (maxRank <= 0) {
                    maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                }
                rankListModel.setRankNum(maxRank);
                rankListModel.setRankList(tempEntry.getValue());
                crossActivityUserRankListModelMap.get(dataEntry.getKey()).put(tempEntry.getKey(), rankListModel);
            }
        }
    }

    private void initCrossActivityRankListModelMap(){
        Map<Integer, ActivityInfo> activityInfoMap = ActivityMgr.getOpenCrossRankActivityMap();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            CrossActivityRankListModel crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), false);
            CrossActivityServerRankListModel crossActivityServerRankListModel = new CrossActivityServerRankListModel(activityInfo.getActivityId());
            int conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
            if(maxRank <= 0){
                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
            }
            crossActivityRankListModel.setRankNum(maxRank);
            LinkedList<CrossUserRank> userRankList = new LinkedList<>();
            LinkedList<ServerRank> serverRankList = new LinkedList<>();
            for(long serverId : activityInfo.getServerIdList()){
                ServerRank serverRank = new ServerRank();
                serverRank.setServerId(serverId);
                BigInteger value = BigInteger.ZERO;
                CrossRankListModel tempModel = getCrossRankListModel(serverId, activityInfo.getActivityId());
                if(null != tempModel){
                    LinkedList<CrossUserRank> tempUserRankList = tempModel.getRankList();
                    for(CrossUserRank tempRank : tempUserRankList){
                        userRankList.add(tempRank.clone());
                    }
                    //只取区服前100名参与区服总分
                    int tempRank = Integer.parseInt(activityInfo.getParam6());
                    if(tempRank > 0){
                        while (tempUserRankList.size() > tempRank){
                            tempUserRankList.removeLast();
                        }
                    }
                    for(CrossUserRank userRank : tempUserRankList){
                        value = value.add(userRank.getValue());
                    }
                }
                serverRank.setValue(value);
                serverRankList.add(serverRank);
            }
            crossActivityRankListModel.setRankList(userRankList);
            crossActivityServerRankListModel.setRankList(serverRankList);
            crossActivityRankListModelMap.put(activityInfo.getActivityId(), crossActivityRankListModel);
            crossActivityServerRankListModelMap.put(activityInfo.getActivityId(), crossActivityServerRankListModel);
        }
    }

    private static Map<Integer, CrossRankListModel> getCrossRankListModelMap(long serverId){
        return allCrossRankListModelMap.get(serverId);
    }

    public static CrossRankListModel getCrossRankListModel(long serverId, int activityId){
        Map<Integer, CrossRankListModel> modelMap  = getCrossRankListModelMap(serverId);
        if(null != modelMap){
            return modelMap.get(activityId);
        }
        return null;
    }

    public static Map<Integer, CrossActivityRankListModel> getCrossActivityNewRankListModelMap(int activityId){
        return crossActivityUserRankListModelMap.get(activityId);
    }

    public static CrossActivityRankListModel getCrossActivityNewRankListModel(int activityId, int conditionType){
        Map<Integer, CrossActivityRankListModel> dataMap = getCrossActivityNewRankListModelMap(activityId);
        if(null != dataMap) {
            return dataMap.get(conditionType);
        }
        return null;
    }

    public static CrossActivityRankListModel getCrossActivityRankListModel(int activityId){
        return crossActivityRankListModelMap.get(activityId);
    }

    public static CrossActivityServerRankListModel getCrossActivityServerRankListModel(int activityId){
        return crossActivityServerRankListModelMap.get(activityId);
    }

    public static CrossUnionActivityRankListModel getCrossActivityUnionRankListModel(int activityId){
        return crossActivityUnionRankListModelMap.get(activityId);
    }

    /**
     * 來自区服商会活动排行数据变化
     *
     * @param activityId
     * @param crossUnionActivityRank
     */
    public static void changeActivityRank(int activityId, CrossUnionActivityRank crossUnionActivityRank, boolean isUnionDel) {
        CrossUnionActivityRankListModel crossUnionActivityRankListModel = getCrossActivityUnionRankListModel(activityId);
        if (null == crossUnionActivityRankListModel) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                getLogger().error("changeActivityRank activityInfo is null activityId:{}", activityId);
                return;
            }
            synchronized (crossActivityUnionRankListModelMap) {
                if (!crossActivityUnionRankListModelMap.containsKey(activityId)) {
                    activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (null != activityInfo) {
                        crossUnionActivityRankListModel = new CrossUnionActivityRankListModel(activityId);
                        crossUnionActivityRankListModel.setRankNum(getMaxRank(activityInfo));
                        crossUnionActivityRankListModel.setActivityId(activityId);
                        crossActivityUnionRankListModelMap.put(activityId, crossUnionActivityRankListModel);
                    }
                }
            }
        }
        if(null != crossUnionActivityRankListModel){
            if(isUnionDel){
                crossUnionActivityRankListModel.removeRank(crossUnionActivityRank.getUnionUid());
            }else {
                crossUnionActivityRankListModel.rankChange(crossUnionActivityRank);
            }

        }
    }

    /**
     * 跨服活动排行数据变化
     *
     * @param activityId
     * @param crossUserRank
     */
    public static void changeActivityRank(int activityId, CrossUserRank crossUserRank) {
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if (null == rankListModel) {
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                getLogger().error("changeActivityRank activityInfo is null activityId:{}", activityId);
                return;
            }
            synchronized (crossActivityRankListModelMap) {
                if (!crossActivityRankListModelMap.containsKey(activityId)) {
                    activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (null != activityInfo) {
                        CrossActivityRankListModel crossActivityRankListModel;
                        int conditionType;
                        if (activityInfo.getType() == eActivityType.CrossCommonActivity.getValue()) {
                            conditionType = ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_CROSS;
                            crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true);
                        } else if (activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue()
                                || activityInfo.getType() == eActivityType.CrossDunHuangShiKuActivity.getValue()) {
                            conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
                            crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true);
                        } else {
                            conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
                            crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), false);
                        }
                        int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if(maxRank <= 0){
                            maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                        }
                        crossActivityRankListModel.setRankNum(maxRank);
                        crossActivityRankListModelMap.put(activityId, crossActivityRankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityRankListModel(activityId);
        }
        if(null != rankListModel){
            rankListModel.rankChange(crossUserRank);
        }
    }

    /**
     * 跨服活动排行数据变化
     */
    public static void changeActivityNewRank(int activityId, int conditionType, CrossUserRank crossUserRank) {
        CrossActivityRankListModel rankListModel = getCrossActivityNewRankListModel(activityId, conditionType);
        if (null == rankListModel) {
            if (!crossActivityUserRankListModelMap.containsKey(activityId)) {
                synchronized (crossActivityUserRankListModelMap) {
                    if (!crossActivityUserRankListModelMap.containsKey(activityId)) {
                        crossActivityUserRankListModelMap.put(activityId, new ConcurrentHashMap<>());
                    }
                }
            }
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (null == activityInfo) {
                getLogger().error("changeActivityNewRank activityInfo is null activityId:{} conditionType:{} ",
                        activityId, conditionType);
                return;
            }
            Map<Integer, CrossActivityRankListModel> dataMap = crossActivityUserRankListModelMap.get(activityId);
            if (!dataMap.containsKey(conditionType)) {
                synchronized (dataMap) {
                    if (!dataMap.containsKey(conditionType)) {
                        if (null != activityInfo) {
                            int maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                            if (maxRank <= 0) {
                                maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                            }
                            CrossActivityRankListModel crossActivityRankListModel;
                            Comparator comparator = ActivityHelper.getUserRankSorter(activityInfo.getType(), conditionType, true);
                            if (comparator == null) {
                                crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true);
                            } else {

                                crossActivityRankListModel = new CrossActivityRankListModel(activityInfo.getActivityId(), true, comparator);
                            }
                            crossActivityRankListModel.setConditionType(conditionType);
                            crossActivityRankListModel.setRankNum(maxRank);
                            dataMap.put(conditionType, crossActivityRankListModel);
                        }
                    }
                }
            }
            rankListModel = getCrossActivityNewRankListModel(activityId, conditionType);
        }
        if(null != rankListModel && null != crossUserRank){
            rankListModel.rankChange(crossUserRank);
        }
    }

    /**
     * 更新各个区服副本排行
     */
    public static void changeUserRank(int activityId, CrossUserRank crossUserRank){
        long serverId = crossUserRank.getUserBaseInfo().getServerId();
        if(0 == serverId){
            return;
        }
        CrossRankListModel rankListModel = getCrossRankListModel(serverId, activityId);
        if (null == rankListModel) {
            synchronized (allCrossRankListModelMap) {
                if(!allCrossRankListModelMap.containsKey(serverId)){
                    allCrossRankListModelMap.put(serverId, new ConcurrentHashMap<>());
                }
                if (!allCrossRankListModelMap.get(serverId).containsKey(activityId)) {
                    rankListModel = new CrossRankListModel(serverId, activityId, eBigRankType.Normal.getValue());
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    int maxRank = 0;
                    if(null != activityInfo) {
                        maxRank = Integer.parseInt(activityInfo.getParam6());
                        int conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
                        int maxRank2 = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
                        if (maxRank2 > maxRank) {
                            maxRank = maxRank2;
                        }
                    }
                    if(maxRank <= 0){
                        maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                    }
                    rankListModel.setRankNum(maxRank);
                    allCrossRankListModelMap.get(serverId).put(activityId, rankListModel);
                }
            }
            rankListModel = getCrossRankListModel(serverId, activityId);
        }
        if (null != rankListModel) {
            rankListModel.rankChange(crossUserRank);
            LinkedList<CrossUserRank> userRankList = rankListModel.getRankList();
            //只取区服前100名参与区服总分
            int tempRank = 0;
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if(null != activityInfo) {
                tempRank = Integer.parseInt(activityInfo.getParam6());
            }
            if(tempRank > 0){
                while (userRankList.size() > tempRank){
                    userRankList.removeLast();
                }
            }
            ServerRank serverRank = new ServerRank();
            serverRank.setServerId(serverId);
            BigInteger value = BigInteger.ZERO;
            for(CrossUserRank userRank : userRankList){
                if (userRank.getValue().compareTo(BigInteger.ZERO) > 0) {
                    value = value.add(userRank.getValue());
                }
            }
            serverRank.setValue(value);
            changeServerRank(activityId, serverRank);
        }
    }

    private static void changeServerRank(int activityId, ServerRank serverRank){
        CrossActivityServerRankListModel rankListModel = getCrossActivityServerRankListModel(activityId);
        if (null == rankListModel) {
            synchronized (crossActivityServerRankListModelMap) {
                if(!crossActivityServerRankListModelMap.containsKey(activityId)){
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if(null != activityInfo) {
                        rankListModel = new CrossActivityServerRankListModel(activityId);
                        LinkedList<ServerRank> serverRankList = new LinkedList<>();
                        for(long serverId : activityInfo.getServerIdList()){
                            ServerRank tempRank = new ServerRank();
                            tempRank.setServerId(serverId);
                            tempRank.setValue(BigInteger.ZERO);
                            serverRankList.add(tempRank);
                        }
                        rankListModel.setRankList(serverRankList);
                        crossActivityServerRankListModelMap.put(activityId, rankListModel);
                    }
                }
            }
            rankListModel = getCrossActivityServerRankListModel(activityId);
        }
        if (null != rankListModel) {
            rankListModel.rankChange(serverRank);
        }
    }

    /**
     * 获取跨服活动名称
     * @param activityId
     * @param conditionType
     * @param serverId
     * @param userId
     * @return
     */
    public static int getNewCrossActivityRank(int activityId, int conditionType, long serverId, long userId) {
        int rank = -1;
        CrossActivityRankListModel rankListModel = getCrossActivityNewRankListModel(activityId, conditionType);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if(null != activityInfo) {
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取用户在跨服冲榜的活动名次
     * @param activityId
     * @param serverId
     * @param userId
     * @return
     */
    public static int getCrossActivityRank(int activityId, long serverId, long userId) {
        int rank = -1;
        CrossActivityRankListModel rankListModel = getCrossActivityRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(userId, serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if(null != activityInfo) {
            int conditionType;
            if(activityInfo.getType() == eActivityType.CrossCommonActivity.getValue()) {
                conditionType = ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_CROSS;
            }else {
                conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
            }
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取区服在某冲榜活动的区服名次
     * @param activityId
     * @param serverId
     * @return
     */
    public static int getCrossActivityServerRank(int activityId, long serverId) {
        int rank = -1;
        CrossActivityServerRankListModel rankListModel = getCrossActivityServerRankListModel(activityId);
        if(null != rankListModel){
            rank = rankListModel.getMyRank(serverId);
        }
        int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if(null != activityInfo) {
            int conditionType = ActivityHelper.getCrossServerRankConditionType(activityInfo.getType(), activityInfo.getChildType());
            maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取商会在跨服活动中的商会名次
     * @param activityId
     * @param unionUid
     * @return
     */
    public static int getCrossActivityUnionRank(int activityId, String unionUid){
        int rank = -1;
        CrossUnionActivityRankListModel crossUnionActivityRankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null != crossUnionActivityRankListModel){
            rank = crossUnionActivityRankListModel.getMyRank(unionUid);
        }

        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        int maxRank = getMaxRank(activityInfo);
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }

    /**
     * 获取跨服活动中的冠军商会
     * @param activityId
     * @return
     */
    public static String getCrossActivityUnionRankTop(int activityId){
        CrossUnionActivityRankListModel crossUnionActivityRankListModel = getCrossActivityUnionRankListModel(activityId);
        if(null != crossUnionActivityRankListModel){
            List<CrossUnionActivityRank> list = crossUnionActivityRankListModel.getRankList();
            if(list.size() > 0){
                return list.get(0).getUnionUid();
            }
        }
        return "";
    }

    /**
     * 获取跨服商会活动中的前n名商会
     */
    public static List<String> getCrossActivityTopUnionsUidList(int activityId, int lowestRank) {
        CrossUnionActivityRankListModel crossUnionActivityRankListModel = getCrossActivityUnionRankListModel(activityId);
        if (null != crossUnionActivityRankListModel) {
            List<CrossUnionActivityRank> list = crossUnionActivityRankListModel.getRankList();
            List<String> unionUIds = new ArrayList<>();
            int rankNum = lowestRank <= list.size() ? lowestRank : list.size();
            for (int i = 0; i < rankNum; i++) {
                unionUIds.add(list.get(i).getUnionUid());
            }
            return unionUIds;
        }
        return null;
    }

}
